<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2019 All rights reserved.
// +----------------------------------------------------------------------
// | User: Simony <1362229026@qq.com>
// | wechat: Shen86579595
// +----------------------------------------------------------------------

namespace app\common\model;

use think\facade\Config;
use think\Model;
use think\Db;

class Base extends Model
{
    public $page_info;

    /**
     * User:Simony
     * Function: 获取上一个使用SQL_CALC_FOUND_ROWS查询时获取到的数据总行数
     * @return float|int
     * @throws \think\db\exception\BindParamException
     * @throws \think\exception\PDOException
     */
    public function foundRows()
    {
        $rows = $this->query('SELECT FOUND_ROWS() AS t');
        return 1 * $rows[0]['t'];
    }

    /**
     * User:Simony
     * Function:
     * @param $condition
     * @return float|string
     */
    public function getComplainCount($condition)
    {
        return $this->where($condition)->count();
    }

    /**
     * User:Simony
     * Function:是否存在
     * @param string $condition
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function isExist($condition = [])
    {
        $list = $this->field('id')->where($condition)->find();
        return $list ? true : false;
    }


    /**
     * User:Simony
     * Function: 获取col
     * @param string $field
     * @param array $where
     * @return array
     */
    public function getCol($field = '*', $where = [])
    {
        return $this->where($where)->column($field);
    }

    /**
     * User:Simony
     * Function: 获取一个值
     * @param string $field
     * @param array $where
     * @return mixed
     */
    public function getVal($field = '', $where = [])
    {
        return $this->where($where)->value($field);
    }

    /**
     * User:Simony
     * Function: 获取一个值
     * @param string $field
     * @param array $where
     * @return mixed
     */
    public function getValByID($id, $field = '')
    {
        $condition = [
            'id' => $id
        ];
        return $this->getVal($field, $condition);
    }

    /**
     * User:Simony
     * Function: 设置字段值
     * @param $condition
     * @param $field
     * @param string $value
     * @return int
     */
    public function setVal($condition, $field, $value = '')
    {
        return $this->where($condition)->setField($field, $value);
    }


    /**
     * User:Simony
     * Function: 设置字段值
     * @param $condition
     * @param $field
     * @param string $value
     * @return int
     */
    public function setValByID($id, $field, $value = '')
    {
        return $this->setVal(['id' => $id], $field, $value);
    }

    /**
     * User:Simony
     * Function: 设置值增加
     * @param $condition
     * @param $field
     * @param int $step
     * @return int|true
     * @throws \think\Exception
     */
    public function setValInc($condition, $field, $step = 1)
    {
        return $this->where($condition)->setInc($field, $step);
    }

    /**
     * User:Simony
     * Function: 设置值增加
     * @param $condition
     * @param $field
     * @param int $step
     * @return int|true
     * @throws \think\Exception
     */
    public function setValIncByID($id, $field, $step = 1)
    {
        return $this->where(['id' => $id])->setInc($field, $step);
    }

    /**
     * User:Simony
     * Function: 设置值减少
     * @param $condition
     * @param $field
     * @param int $step
     * @return int|true
     * @throws \think\Exception
     */
    public function setValDec($condition, $field, $step = 1)
    {
        return $this->where($condition)->setDec($field, $step);
    }


    /**
     * User:Simony
     * Function: 设置值减少
     * @param $condition
     * @param $field
     * @param int $step
     * @return int|true
     * @throws \think\Exception
     */
    public function setValDecID($id, $field, $step = 1)
    {
        return $this->where(['id' => $id])->setDec($field, $step);
    }

    /**
     * User:Simony
     * Function: 添加
     * @param $data
     * @return int|string
     */
    public function add($data)
    {
        return $this->insertGetId($data);
    }

    /**
     * User:Simony
     * Function: 批量添加
     * @param $data
     */
    public function addAll($data)
    {
        return $this->insertAll($data);
    }


    /**
     * User:Simony
     * Function: 编辑
     * @param $condition
     * @param $update
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function edit($condition, $update)
    {
        return $this->where($condition)->update($update);
    }

    /**
     * User:Simony
     * Function: 通过id更新
     * @param $id
     * @param $update
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function editByID($id, $update)
    {
        return $this->edit(['id' => $id], $update);
    }

    /**
     * User:Simony
     * Function:记录数量
     * @param $condition
     * @return float|string
     */
    public function getCount($condition = [], $field = 'id')
    {
        return $this->where($condition)->count();
    }

    /**
     * User:Simony
     * Function:最大值
     * @param string $field
     * @param $condition
     * @return mixed|\think\db\Query
     */
    public function getMax($condition, $field)
    {
        return $this->where($condition)->max($field);
    }

    /**
     * User:Simony
     * Function:最小值
     * @param string $field
     * @param $condition
     * @return mixed|\think\db\Query
     */
    public function getMin($condition, $field)
    {
        return $this->where($condition)->min($field);
    }

    /**
     * User:Simony
     * Function:
     * @param string $field
     * @param $condition
     * @return mixed|\think\db\Query
     */
    public function getSum($condition, $field)
    {
        return $this->where($condition)->sum($field);
    }

    /**
     * User:Simony
     * Function:平均值
     * @param string $field
     * @param $condition
     * @return mixed|\think\db\Query
     */
    public function getAvg($condition, $field)
    {
        return $this->where($condition)->avg($field);
    }

    /**
     * User:Simony
     * Function: 列表
     * @param $condition
     * @param int $page
     * @param string $field
     * @param string $order
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList($condition = [], $page = -1, $field = '*', $order = 'id desc')
    {
        $res = $this->where($condition)
            ->field($field)
            ->order($order);

        if ($page == -1) {
            $page = Config::get('paginate.list_rows');
        }
        if ($page) {
            $res = $res->paginate($page, false, ['query' => request()->param()]);
            $this->page_info = $res;
            $res = $res->toArray();
            return $res['data'];
        } else {
            return $res->select()->toArray();
        }
    }

    /**
     * User:Simony
     * Function: 查询单条数据
     * @param $condition
     * @param string $fields
     * @return array|null|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getInfo($condition, $fields = '*')
    {
        return $this->field($fields)->where($condition)->find();
    }

    /**
     * User:Simony
     * Function:通过id查找信息
     * @param $id
     * @return array|null|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getInfoByID($id, $fields = '*')
    {
        return $this->getInfo(['id' => $id], $fields);
    }

    /**
     * User:Simony
     * Function: 删除数据
     * @param $condition
     * @return int
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del($condition)
    {
        return $this->where($condition)->delete();
    }

    /**
     * User:Simony
     * Function: 按id删除数据
     * @param $id
     * @return int
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function delByID($id)
    {
        return $this->del(['id' => $id]);
    }


}

?>
